﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BUS;
using DTO;

namespace GUI
{
    public partial class frmLuaChonKhachHang : DevComponents.DotNetBar.Metro.MetroForm
    {
        public delegate void LayKhachHang(KhachHangDTO khDTO);
        public event LayKhachHang KhiLayKhachHang = null;

        KhachHangDTO LayThongTin()
        {
            int ma = int.Parse(dgvKhachHang.CurrentRow.Cells[0].Value.ToString());
            string TenKhachHang = dgvKhachHang.CurrentRow.Cells[2].Value.ToString();
            KhachHangDTO khDTO = new KhachHangDTO();
            khDTO.MaKhachHang = ma;
            khDTO.TenKhachHang = TenKhachHang;
            return khDTO;
        }
        public frmLuaChonKhachHang()
        {
            InitializeComponent();
        }

        private void frmLuaChonKhachHang_Load(object sender, EventArgs e)
        {
            LoadKhachHang();
        }
        private void LoadKhachHang()
        {
            KhachHangBUS khBUS = new KhachHangBUS();
            List<KhachHangDTO> listKH = khBUS.LayDanhSachKhachHang();
            this.dgvKhachHang.Rows.Clear();
            for (int i = 0; i < listKH.Count; i++)
            {
                dgvKhachHang.Rows.Add(listKH[i].MaKhachHang, (i + 1).ToString(), listKH[i].TenKhachHang, listKH[i].SoDienThoai, listKH[i].DiaChi);
            }
        }

        private void btnThemKhachHang_Click(object sender, EventArgs e)
        {
            frmThemKhachHang frm = new frmThemKhachHang();
            frm.KhiThemMoi += frm_KhiThemMoiKhachHang;
            frm.ShowDialog();
        }

        void frm_KhiThemMoiKhachHang(KhachHangDTO nvDTO)
        {
            KhachHangBUS khBUS = new KhachHangBUS();
            khBUS.ThemKhachHang(nvDTO);
            LoadKhachHang();
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            KhachHangBUS khBUS = new KhachHangBUS();
            List<KhachHangDTO> listKH = khBUS.TimKiemKhachHang(txtTimKiem.Text);
            this.dgvKhachHang.Rows.Clear();
            for (int i = 0; i < listKH.Count; i++)
            {
                dgvKhachHang.Rows.Add(listKH[i].MaKhachHang, (i + 1).ToString(), listKH[i].TenKhachHang, listKH[i].SoDienThoai, listKH[i].DiaChi);
            }
        }

        private void dgvKhachHang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.ColumnIndex == 5)
            {
                if (e.RowIndex < 0)
                {
                    return;
                }
                KhachHangDTO khDTO = LayThongTin();
                if (KhiLayKhachHang != null)
                {
                    KhiLayKhachHang(khDTO);
                }
                this.Close();
            }
        }
    }
}